package org.streets.eis.setting.auth;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.streets.commons.expression.ExpressValueProvider;
import org.streets.database.Limits;
import org.streets.database.SQLConnection;

import org.streets.eis.entity.Role;
/**
 * 
 * @author dzb
 *
 */
public class UserRoleValueProvider implements ExpressValueProvider{

	private Map<String, Integer> ROLE_VALUES = new HashMap<String, Integer>();
	
	public UserRoleValueProvider(SQLConnection connection) {
		reload(connection);
	}
	
	public Object value(String varTag) {
		
		Object v = ROLE_VALUES.get(varTag);
		if (v == null) {
			v = varTag;
		}		
		return v;
	}

	public void reload(SQLConnection connection) {
		List<Role> roles = connection.query("select * from eis_roles", Role.class, Limits.none());
		for (Role role : roles) {
			ROLE_VALUES.put(role.getId(), role.getValue());
		}
	}
}
